
/********************************************************************************
** This file contains the definition of OpenQ10 TextEdit
**
** Created: Fri 30. Nov 09:45:35 2007
**      by: Imran Shahid
**
********************************************************************************/

#include "textedit.h"

#include <QAction>
#include <QApplication>
#include <QClipboard>
#include <QBrush>
#include <QColor>
#include <QFont>
#include <QPalette>
#include <QTextCodec>
#include <QTextEdit>
#include <QTextCursor>
#include <QtDebug>

TextEdit::TextEdit(QWidget *parent) : QTextEdit(parent)
{
//    setupActions();

    this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    this->setWordWrapMode(QTextOption::WordWrap);
    this->setFocus();
} // TextEdit

void TextEdit::setupActions()
{
    connect(this->document(), SIGNAL(modificationChanged(bool)),
            this, SLOT(setWindowModified(bool)));
    connect(this->document(), SIGNAL(undoAvailable(bool)),
            actionUndo, SLOT(setEnabled(bool)));
    connect(this->document(), SIGNAL(redoAvailable(bool)),
            actionRedo, SLOT(setEnabled(bool)));

    setWindowModified(this->document()->isModified());
    actionUndo->setEnabled(this->document()->isUndoAvailable());
    actionRedo->setEnabled(this->document()->isRedoAvailable());

    connect(actionUndo, SIGNAL(triggered()), this, SLOT(undo()));
    connect(actionRedo, SIGNAL(triggered()), this, SLOT(redo()));

    actionCut->setEnabled(false);
    actionCopy->setEnabled(false);

    connect(actionCut, SIGNAL(triggered()), this, SLOT(cut()));
    connect(actionCopy, SIGNAL(triggered()), this, SLOT(copy()));
    connect(actionPaste, SIGNAL(triggered()), this, SLOT(paste()));

    connect(this, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
    connect(this, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));

    connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(clipboardDataChanged()));
} // setupActions

void TextEdit::clipboardDataChanged()
{
    actionPaste->setEnabled(!QApplication::clipboard()->text().isEmpty());
} // clipboardDataChanged

void TextEdit::textColorChanged(QColor *color)
{
    QPalette palette;
    QBrush brush(color->rgb());
    QBrush brushHtBkGd(QColor(85, 85, 127, 255));   // try this also QColor(10, 36, 106, 255)

    brush.setStyle(Qt::SolidPattern);
    brushHtBkGd.setStyle(Qt::SolidPattern);

    palette.setBrush(QPalette::Active, QPalette::Text, brush);
    palette.setBrush(QPalette::Active, QPalette::Highlight, brushHtBkGd);
    palette.setBrush(QPalette::Active, QPalette::HighlightedText, brush);

    this->setPalette(palette);
} // fontColorChanged

void TextEdit::backgroundColorChanged(QColor *color)
{
    QPalette palette;
    QBrush brush(color->rgb());
    brush.setStyle(Qt::SolidPattern);
    palette.setBrush(QPalette::Active, QPalette::Base, brush);
    this->setPalette(palette);
} // backgroundColorChanged

void TextEdit::fontChanged(QFont *font)
{
    this->setFont(*font);
} // fontChanged

